﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class MenuDAO
    {
        DataServices m_MenuDAO = new DataServices();

        public DataTable GetAll()
        {
            m_MenuDAO.Load("GetAllMenu", null);
            return m_MenuDAO;
        }

        public DataTable GetById(String menuId)
        {
            SqlParameter param = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param.Value = menuId;

            m_MenuDAO.Load("GetMenuById", param);
            return m_MenuDAO;
        }

        public DataTable GetByLikeId(String menuId)
        {
            SqlParameter param = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param.Value = menuId;

            m_MenuDAO.Load("GetMenuByLikeId", param);
            return m_MenuDAO;
        }

        public DataTable GetByName(String menuName)
        {
            SqlParameter param = new SqlParameter("@MenuName", SqlDbType.NVarChar, 200);
            param.Value = menuName;
            m_MenuDAO.Load("GetMenuByName", param);
            return m_MenuDAO;
        }

        public DataTable GetByCate(String cateId)
        {
            SqlParameter param = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param.Value = cateId;
            m_MenuDAO.Load("GetMenuByCate", param);
            return m_MenuDAO;
        }

        public DataTable GetByState(int state)
        {
            SqlParameter param = new SqlParameter("@State", SqlDbType.Int);
            param.Value = state;
            m_MenuDAO.Load("GetMenuByState", param);
            return m_MenuDAO;
        }

        public DataTable GetByPrice(Double start, Double end)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Start", SqlDbType.Real);
            param[0].Value = start;

            param[1] = new SqlParameter("@End", SqlDbType.Real);
            param[1].Value = end;
            m_MenuDAO.Load("GetMenuByPrice", param);
            return m_MenuDAO;
        }

        public String GetUnitId(String menuId)
        {
            SqlParameter param = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param.Value = menuId;

            String sReturn = Convert.ToString(m_MenuDAO.ExecuteScalar("GetUnitByMenuId", param));
            return sReturn;
        }

        public DataTable GetByOption(String menuId, String menuName, String cateId, String unitId, bool price, double minPrice, double maxPrice)
        {
            SqlParameter[] param = new SqlParameter[7];

            param[0] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[0].Value = menuId;

            param[1] = new SqlParameter("@MenuName", SqlDbType.NVarChar, 200);
            param[1].Value = menuName;

            param[2] = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param[2].Value = cateId;

            param[3] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[3].Value = unitId;

            param[4] = new SqlParameter("@Price", SqlDbType.Bit);
            param[4].Value = price;

            param[5] = new SqlParameter("@PriceMin", SqlDbType.Money);
            param[5].Value = minPrice;

            param[6] = new SqlParameter("@PriceMax", SqlDbType.Money);
            param[6].Value = maxPrice;

            m_MenuDAO.Load("GetMenuFull", param);
            return m_MenuDAO;
        }

        public int Add(MenuInfo info)
        {
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[0].Value = info.MenuId;

            param[1] = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param[1].Value = info.CateId;

            param[2] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[2].Value = info.UnitId;

            param[3] = new SqlParameter("@MenuName", SqlDbType.NVarChar, 200);
            param[3].Value = info.MenuName;

            param[4] = new SqlParameter("@Price", SqlDbType.Money);
            param[4].Value = info.Price;

            param[5] = new SqlParameter("@State", SqlDbType.Int);
            param[5].Value = info.State;

            param[6] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[6].Value = info.Remark;

            return m_MenuDAO.ExecuteUpdate("AddMenu", param);
        }

        public int Update(MenuInfo info)
        {
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[0].Value = info.MenuId;

            param[1] = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param[1].Value = info.CateId;

            param[2] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[2].Value = info.UnitId;

            param[3] = new SqlParameter("@MenuName", SqlDbType.NVarChar, 200);
            param[3].Value = info.MenuName;

            param[4] = new SqlParameter("@Price", SqlDbType.Real);
            param[4].Value = info.Price;

            param[5] = new SqlParameter("@State", SqlDbType.Int);
            param[5].Value = info.State;

            param[6] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[6].Value = info.Remark;

            return m_MenuDAO.ExecuteUpdate("UpdateMenu", param);
        }

        public int Delete(String menuId)
        {
            SqlParameter param = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param.Value = menuId;

            return m_MenuDAO.ExecuteUpdate("DeleteMenu", param);
        }

        public DataRow AddNewRow()
        {
            return m_MenuDAO.NewRow();
        }

        public void AddNewMenu(DataRow m_Row)
        {
            m_MenuDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_MenuDAO.ExecuteNoneQuery() > 0;
        }
    }
}
